package br.fatec.ads.servlet;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.fatec.ads.dao.CurriculoDAO;
import br.fatec.ads.dao.ExperienciaDAO;
import br.fatec.ads.dao.FormacaoDAO;
import br.fatec.ads.exception.CurriculoException;
import br.fatec.ads.exception.DataBaseException;
import br.fatec.ads.exception.ExperienciaException;
import br.fatec.ads.exception.FormacaoException;
import br.fatec.ads.model.Curriculo;
import br.fatec.ads.model.CurriculoPK;
import br.fatec.ads.model.Experiencia;
import br.fatec.ads.model.Formacao;
import br.fatec.ads.model.Usuario;

public class CurriculoIncluir extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public CurriculoIncluir() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("cadcurriculo_i.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			HttpSession session = request.getSession(false);
			DateFormat fmt = new SimpleDateFormat("dd/MM/yy");
			int idCurriculo = 0;
			
			Curriculo curriculo = new Curriculo();
			CurriculoPK curriculoPK = new CurriculoPK();
			Usuario usuario = new Usuario();
			Formacao[] formacao = new Formacao[3];
			Experiencia[] experiencia = new Experiencia[3];
			
			CurriculoDAO curriculoDAO = CurriculoDAO.getInstance();
			FormacaoDAO formacaoDAO = FormacaoDAO.getInstance();
			ExperienciaDAO experienciaDAO = ExperienciaDAO.getInstance();
			
			usuario.setIdUsuario(Integer.parseInt(session.getAttribute("idUsuario").toString()));
			
			curriculo.setUsuario(usuario);
			curriculo.setNacionalidade(request.getParameter("txtNacionalidade"));
			curriculo.setDataNascimento(new Date(fmt.parse(request.getParameter("txtDtNascimento")).getTime()));
			curriculo.setEstadoCivil(request.getParameter("cmbEstadoCivil"));
			curriculo.setQtdeFilhos(Integer.parseInt(request.getParameter("txtQtdFilhos")));
			curriculo.setEmail(request.getParameter("txtEmail"));
			curriculo.setEmailSecundario(request.getParameter("txtEmailSecundario"));
			curriculo.setEndereco(request.getParameter("txtEndereco"));
			curriculo.setBairro(request.getParameter("txtBairro"));
			curriculo.setCep(request.getParameter("txtCep").replace("-", ""));
			curriculo.setCidade(request.getParameter("txtCidade"));
			curriculo.setUf(request.getParameter("txtUf"));
			curriculo.setPais(request.getParameter("txtPais"));
			curriculo.setDddResidencia(request.getParameter("txtDddRes").equals("") ? 0 : Integer.parseInt(request.getParameter("txtDddRes")));
			curriculo.setTelResidencia(request.getParameter("txtTelRes").equals("") ? "" : request.getParameter("txtTelRes").replace("-", ""));
			curriculo.setDddComercial(request.getParameter("txtDddComer").equals("") ? 0 : Integer.parseInt(request.getParameter("txtDddComer")));
			curriculo.setTelefoneComercial(request.getParameter("txtTelComer").equals("") ? "" : request.getParameter("txtTelComer").replace("-", ""));
			curriculo.setDddCelular(request.getParameter("txtDddCel").equals("") ? 0 : Integer.parseInt(request.getParameter("txtDddCel")));
			curriculo.setTelCelular(request.getParameter("txtTelCel").equals("") ? "" : request.getParameter("txtTelCel").replace("-", ""));
			curriculo.setIndDeficiente(request.getParameter("cmbIndDeficiente"));
			curriculo.setDescDeficiencia(request.getParameter("txtDescDeficiente"));
			curriculo.setObjetivoProfissional(request.getParameter("txtObjetivoProfissional"));
			curriculo.setPretencaoSalarial(new BigDecimal(request.getParameter("txtPretensaoSalarial").replace(",", ".")));
			
			curriculoDAO.incluirCurriculo(curriculo);
			
			idCurriculo = curriculoDAO.getUltimoCurriculo(usuario.getIdUsuario());
			curriculoPK.setIdCurriculo(idCurriculo);
			
			for(int i = 0; i < 3; i++){
				formacao[i] = new Formacao();
				formacao[i].setCurriculoPK(curriculoPK);
				formacao[i].setInstituicao(request.getParameter("txtInstituicao0" + (i + 1)));
				formacao[i].setCidadeInstituicao(request.getParameter("txtCidadeInstituicao0" + (i + 1)));
				formacao[i].setEstadoInstituicao(request.getParameter("txtUfInstituicao0" + (i + 1)));
				formacao[i].setPaisInstituicao(request.getParameter("txtPaisInstituicao0" + (i + 1)));
				formacao[i].setTipoFormacao(request.getParameter("txtTipoFormacao0" + (i + 1)));
				formacao[i].setCurso(request.getParameter("txtCurso0" + (i + 1)));
				formacao[i].setDuracaoHoras(request.getParameter("txtDuracaoHoras0" + (i + 1)).equals("") ? 0 : Integer.parseInt(request.getParameter("txtDuracaoHoras0" + (i + 1))));
				formacao[i].setDataInicio(request.getParameter("txtDtInicio0" + (i + 1)).equals("") ? null : new Date(fmt.parse(request.getParameter("txtDtInicio0" + (i + 1))).getTime()));
				formacao[i].setDataFim(request.getParameter("txtDtFim0" + (i + 1)).equals("") ? null : new Date(fmt.parse(request.getParameter("txtDtFim0" + (i + 1))).getTime()));
				formacao[i].setIndSituacao(request.getParameter("cmbSatusFormacao0" + (i + 1)));
				
				if(!(formacao[i].getDataInicio() == null)){
					formacaoDAO.incluirFormacao(formacao[i]);
				}
			}

			for(int i = 0; i < 3; i++){
				experiencia[i] = new Experiencia();
				experiencia[i].setIdCurriculo(curriculoPK);
				experiencia[i].setPais(request.getParameter("txtPais0" + (i + 1)));
				experiencia[i].setEstado(request.getParameter("txtUf0" + (i + 1)));
				experiencia[i].setPorte(request.getParameter("txtPorte0" + (i + 1)));
				experiencia[i].setSegmento(request.getParameter("txtSegmento0" + (i + 1)));
				experiencia[i].setCargo(request.getParameter("txtCargo0" + (i + 1)));
				experiencia[i].setDescAtividade(request.getParameter("txtDescAtividade0" + (i + 1)));
				experiencia[i].setDataInicio(request.getParameter("txtDtInicioE0" + (i + 1)).equals("") ? null : new Date(fmt.parse(request.getParameter("txtDtInicioE0" + (i + 1))).getTime()));
				experiencia[i].setDataFim(request.getParameter("txtDtFimE0" + (i + 1)).equals("") ? null : new Date(fmt.parse(request.getParameter("txtDtFimE0" + (i + 1))).getTime()));
				
				if(!(experiencia[i].getDataInicio() == null)){
					experienciaDAO.incluirExperiencia(experiencia[i]);
				}
			}
			
			request.getRequestDispatcher("CurriculoConsultar?acao=SUCESSO_INCLUIR").forward(request, response);
		} catch (DataBaseException e) {
			request.getRequestDispatcher("CurriculoConsultar?acao=ERRO_INCLUIR").forward(request, response);
			e.printStackTrace();
		} catch (CurriculoException e) {
			request.getRequestDispatcher("CurriculoConsultar?acao=ERRO_INCLUIR").forward(request, response);
			e.printStackTrace();
		} catch (FormacaoException e) {
			request.getRequestDispatcher("CurriculoConsultar?acao=ERRO_INCLUIR").forward(request, response);
			e.printStackTrace();
		} catch (ExperienciaException e) {
			request.getRequestDispatcher("CurriculoConsultar?acao=ERRO_INCLUIR").forward(request, response);
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
}